const mongoose = require("mongoose");

mongoose
  .connect("mongodb://127.0.0.1:27017/groupProject")
  .then(() => {
    console.log("✅ 数据库连接成功");
  })
  .catch(() => {
    console.error("❌ 数据库连接失败:");
  });

//用户
const users = new mongoose.Schema({
  //用户名
  user: String,
  //密码
  pwd: String,
  //电话
  phone: Number,
  //创建事件
  time: {
    type: Date,
    default: Date.now,
  },
});

//广告
const advertise = new mongoose.Schema({
  //图片
  img: String,
  //名称
  name: String,
  //提示语
  prompt: String,
  //数量
  number: Number,
  //状态
  status: Boolean,
  //创建事件
  time: {
    type: Date,
    default: Date.now,
  },
});

//工人类型
const workerType = new mongoose.Schema({
  //工种名称
  name: String,
  //状态
  status: Boolean,
  num: Number,
  //创建事件
  time: {
    type: Date,
    default: Date.now,
  },
});

//工人
const worker = new mongoose.Schema({
  //名称
  name: String,
  //工种
  wId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "workerType",
  },
  //状态
  status: {
    type: String,
    enum: ["未审核", "已通过", "未通过"], //  限定三个值
    default: "未审核",
  },
  //创建事件
  time: {
    type: Date,
    default: Date.now,
  },
  sex: Boolean, //男为true 女为false，
  //电话
  phone: String,
  //地址
  address: String,
  //保险
  insurance: Boolean,
  //民族
  nationality: {
    type: String,
    default: "民族",
  },
  //积累接单
  numOne: {
    type: Number,
    default: 0,
  },
  //状态
  status: {
    type: String,
    enum: ["未审核", "已通过", "未通过"], //  限定三个值
    default: "未审核",
  },
  //总收入
  worth:Number
});

//雇主
const employer = new mongoose.Schema({
  name: String, //名称
  // 电话
  phone: String,
  //创建事件
  time: {
    type: Date,
    default: Date.now,
  },
  //总资金
  worth: Number,
});

//雇主发布过的任务
const emps = new mongoose.Schema({
  //雇主id
  eId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "employer",
  },
  //任务名称
  title: String,
  //工种
  wId: {
    type: mongoose.Schema.Types.ObjectId,
    ref: "workerType",
  },
  //任务的钱数
  salary: Number,
  //已经结算
  endPrice: {
    type: Number,
    default: 0,
  },
  //状态
  status: {
    type: String,
    enum: ["招工中", "未支付", "已完工"], //  限定三个值
    default: "招工中",
  },
  //招工人数
  wNum: Number,
  //创建事件
  time: {
    type: Date,
    default: Date.now,
  },
});

//雇主资金流水
const  funds =  new mongoose.Schema({
    empId: {
      type: mongoose.Schema.Types.ObjectId,
      ref: "employer",
    },
    //总金额
    salaryId:{
        type: mongoose.Schema.Types.ObjectId,
        ref: "emps",
    },
})
//提现审核
const  withdraw = new mongoose.Schema({
    nameId:{
        type: mongoose.Schema.Types.ObjectId,
        ref: "worker",
    },
    //金额
    price:Number,
    //时间
    time: {
      type: Date,
      default: Date.now,
    },
    //状态
    status: {
      type: String,
      enum: ["未审核", "已通过", "未通过"], //  限定三个值
      default: "未审核",
    },
    //备注
    remark:String,
    //总金额
    worth:{
      type: mongoose.Schema.Types.ObjectId,
        ref: "worker", 
    }
})


const usersModel = mongoose.model("users", users, "users");
const advertiseModel = mongoose.model("advertise", advertise, "advertise");
const workerTypeModel = mongoose.model("workerType", workerType, "workerType");
const workerModel = mongoose.model("worker", worker, "worker");
const employerModel = mongoose.model("employer", employer, "employer");
const empsModel = mongoose.model("emps", emps, "emps");
const withdrawModel = mongoose.model('withdraw',withdraw,"withdrawModel")
const fundsModel = mongoose.model('funds',funds,"fundsModel")
module.exports = { usersModel, advertiseModel, workerTypeModel, workerModel, employerModel, empsModel,withdrawModel,fundsModel };
